<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>项目类别</title>
<link type="text/css"
	href="${pageContext.request.contextPath}/assets/plugins/jquery-easyui-1.4.2/themes/metro/easyui.css"
	rel="stylesheet" />
<link type="text/css"
	href="${pageContext.request.contextPath}/assets/plugins/jquery-easyui-1.4.2/themes/mobile.css"
	rel="stylesheet" />
<link type="text/css"
	href="${pageContext.request.contextPath}/assets/css/common.css"
	rel="stylesheet" />
<style type="text/css">
html, body {
	/* height: 100%; */
}

/*
body {
	padding-right: 10px;
}*/

li {
	list-style: none;
}

.main_left {
	width: 200px;
	/* float: left; */
	border: solid 1px #eee;
	position:absolute;
	top:8px;
	bottom:5px;
	/* height: 98%; */
}
.main_left ul{
	color:#555;
}

.title {
	display: block;
	border-bottom: solid 1px #eee;
	padding: 7px 0px 7px 10px;
	font-size: 12px;
	color:#555;
}

.main_right {
	left: 205px;
	border: solid 1px #eee;
	position:absolute;
	top:8px;
	bottom:5px;
	right:5px;
	/*margin-right: 15px;*/
	/* height: 98%; */
}

.main_right .content {
	padding-left: 10px;
}

.tab-content {
	border: none;
}

</style>

</head>
<body>
	<div class="main_left">
		<span class="title">项目类别</span>
		<ul id="tt"></ul>
	</div>
	<div class="main_right">
		<span class="title">项目类别编辑</span>
		<!--内容-->

		<div class="tab-content">
			<dl>
				<dt>上级项目类别</dt>
				<dd>
					<input id="parent" style="height: 32px;" name="PARENT_CODE"
						class="input normal"></input>
				</dd>
			</dl>
			<dl>
				<dt>项目类别编码</dt>
				<dd>
					<input id="categoryId" name="CODE" disabled="disabled" class="input normal"></input> <span
						class="Validform_checktip">系统自动生成，不可修改</span>
				</dd>
			</dl>
			<dl>
				<dt>项目类别名称</dt>
				<dd>
					<input id="categoryName" name="NAME" class="input normal"></input>
					<span class="Validform_checktip">*必填</span>
				</dd>
			</dl>
			<dl>
				<dt>备注说明</dt>
				<dd>
					<textarea id="txtRemark" name="MEMO" class="input"></textarea>
					<span class="Validform_checktip">非必填，最多500个字符</span>
				</dd>
			</dl>
		</div>
		<!--/内容-->

		<button class="btn yellow" style="margin-left: 80px;" onclick="add()">新增类别</button>
		<button class="btn grey" onclick="del()">删除类别</button>
		<button class="btn" onclick="save()">提交保存</button>

	</div>
	<script
		src="${pageContext.request.contextPath}/assets/js/jquery-1.11.2.min.js"
		charset="utf-8" type="text/javascript"></script>
	<script
		src="${pageContext.request.contextPath}/assets/plugins/jquery-easyui-1.4.2/jquery.easyui.min.js"
		charset="utf-8" type="text/javascript"></script>
	<script
		src="${pageContext.request.contextPath}/assets/plugins/jquery-easyui-1.4.2/locale/easyui-lang-zh_CN.js"
		charset="utf-8" type="text/javascript"></script>
	<script src="${pageContext.request.contextPath}/assets/js/common.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
	var contextPath = '<%=request.getContextPath()%>';
		$(function() {
			getTree();
			getParent();
			$('#categoryId').blur(function(){
				if(action=='add'){
					if(!$.isNullOrEmpty($('#categoryId').val())){
						$.ajax({
							url : contextPath + "/bg/IC103.do",
							data : {
								"id" : $('#categoryId').val(),
								"rnd" : Math.random()
							},
							dataType : 'json',
							success : function(data, textStatus, jqXHR) {
								if(data.status=='true'){
									$.messager.show({title:'提示',msg:'项目类别编码已存在！',showType:'slide'});
									$('#categoryId').val('');
									$('#categoryId').focus();
								}				
							}
						})
					}
				}
			});
		})
		//获取左边树
		function getTree() {
			$.ajax({
				url : contextPath + "/bg/IC101.do",
				data : {
					"rnd" : Math.random()
				},
				dataType : 'json',
				success : function(data, textStatus, jqXHR) {
					var result = data.result;
					var json = '';
					for (var i = 0; i < result.length; i++) {
						if (result[i].level == '1') {
							json += '{"id":"' + result[i].id + '","level":'
									+ result[i].level + ',"text":"'
									+ result[i].name + '"';
							var parent = result[i];
							var childJson = '';
							var num = 0;
							for (var j = 0; j < result.length; j++) {
								if (parent.id == result[j].parent
										&& result[j].level == '2') {
									childJson += '{"id":"' + result[j].id
											+'","level":' + result[j].level
											+ ',"text":"' + result[j].name
											+ '"},';
									num++;
								}
							}
							if (num > 0) {
								childJson = childJson.substring(0,
										childJson.length - 1);
								json += ',"children":[' + childJson + ']},';
							} else {
								json += '},'
							}
						}
					}
					json = '[' + json.substring(0, json.length - 1) + ']';
					$('#tt').tree({
						data : eval("(" + json + ")"),
						onClick : function(node) {
							if (node.level == '2') {
								edit(node.id);
							}
						}
					});
				}
			});
		}
		//获取项目大类combobox
		function getParent() {
			$.ajax({
				url : contextPath + "/bg/IC102.do",
				data : {
					"rnd" : Math.random()
				},
				dataType : 'json',
				success : function(data, textStatus, jqXHR) {
					var result = data.result;
					var json = '';
					for (var j = 0; j < result.length; j++) {
						json += '{"id":"' + result[j].id + '","text":"'
								+ result[j].name + '"},'
					}
					if (json.length > 0) {
						json = json.substring(0, json.length - 1);
					}
					json = '[' + json + ']';
					$('#parent').combobox({
						data : eval("(" + json + ")"),
						valueField : 'id',
						textField : 'text'
					});
				}
			})
		}
		var action = '';
		//新增
		function add() {
			action = 'add';
			getMaxCategory();
			$('#categoryName').val('');
			$('#txtRemark').val('');
			$('#parent').combobox('clear');
		}
		//编辑
		function edit(id) {
			action = 'edit';
			$('#categoryId').val(id);
			$('#categoryName').val('');
			$('#txtRemark').val('');
			$.ajax({
				url : contextPath + "/bg/IC103.do",
				data : {
					"id" : id,
					"rnd" : Math.random()
				},
				dataType : 'json',
				success : function(data, textStatus, jqXHR) {
					if(data.status=='true'){
						var record=data.record;
						$('#categoryName').val(record.name);
						$('#txtRemark').val(record.memo);
						$('#parent').combobox('setValue', record.parent);
					}else{
						$.messager.show({title:'提示',msg:'查询项目类别错误！',showType:'slide'});
					}				
				}
			})
		}
		//保存
		function save(){
			var CODE = $('#categoryId').val();
			var NAME = $('#categoryName').val();
			var PARENT_CODE = $('#parent').combobox('getValue');
			var MEMO=$('#txtRemark').val();	
			$.ajax({
				url : contextPath + "/bg/IC104.do",
				data : {
					"CODE" : CODE,
					"NAME":NAME,
					"PARENT_CODE":PARENT_CODE,
					"MEMO":MEMO,
					"action":action,
					"rnd" : Math.random()
				},
				dataType : 'json',
				success : function(data, textStatus, jqXHR) {
					if(data.status=='true'){
						$.messager.show({title:'提示',msg:'保存成功！',showType:'slide'});
						getTree();
					}else{
						$.messager.show({title:'提示',msg:'保存失败！',showType:'slide'});
					}
				}
			})
		}
		//删除类别
		function del(){
			var CODE = $('#categoryId').val();
			if($.isNullOrEmpty(CODE)){
				$.messager.show({title:'提示',msg:'请选择需要删除的类别！',showType:'slide'});
				return;
			}
		    $.messager.confirm('确认', '确认删除此类别及其相关所有项目？', function(r){
		    	if (r){
					$.ajax({
						url : contextPath + "/bg/IC105.do",
						data : {
							"CODE" : CODE,
							"rnd" : Math.random()
						},
						dataType : 'json',
						success : function(data, textStatus, jqXHR) {
							getTree();
							$('#categoryId').val('');
							$('#categoryName').val('');
							$('#txtRemark').val('');
							$('#parent').combobox('clear');
						}
					})
		    	}else{
		    		return;
		    	}
		    });
		}
		//获取项目大类编码
		function getMaxCategory(){
			$.ajax({
				url : contextPath + "/bg/IC106.do",
				type:"post",
				data : {
					"rnd" : Math.random()
				},
				dataType : 'json',
				success : function(data, textStatus, jqXHR) {
					$('#categoryId').val(data.MAX_CODE);
				}
			})
		}
	</script>
</body>
</html>